//
// Created by fanghr on 17-12-25.
//

#ifndef PROJECT_SESSION_H
#define PROJECT_SESSION_H

#include "../include.h"

class session : public boost::enable_shared_from_this<session> {
private:
    boost::asio::ip::tcp::socket socket_;
public:
    session(boost::asio::io_service &io_service);

    ~session();

    boost::asio::ip::tcp::socket &socket();

    void start();

    void stop();

    typedef uint32_t (*callback_t)(boost::asio::streambuf &);

    void set_callback(callback_t);

private:

    callback_t callback;

    uint32_t size;

    boost::asio::streambuf buf;

    void handle_request(boost::asio::streambuf &buf, boost::system::error_code ec);

    void async_read_buf();

    void handle_read_size(boost::system::error_code ec);

    void async_write_buf(boost::asio::streambuf &buf, uint32_t size);

    void handle_write_size(boost::asio::streambuf &buf, boost::system::error_code ec);

    void write_cb(boost::system::error_code ec);
};


#endif //PROJECT_SESSION_H
